'use client';

import React, { useState } from 'react';
import { useRouter } from 'next/navigation';
import {
  Form,
  Input,
  Button,
  Card,
  Typography,
  message,
  Space,
  Divider,
} from 'antd';
import {
  UserOutlined,
  LockOutlined,
  SafetyCertificateOutlined,
} from '@ant-design/icons';
import { login as authLogin } from '@/apis/auth';
import { useAuth } from '@/app/admin/components/AuthContext';
import type { LoginRequest } from '@/types/auth';
import styles from './login.module.css';

const { Title, Text } = Typography;

export default function LoginPage() {
  const [loading, setLoading] = useState(false);
  const [form] = Form.useForm();
  const router = useRouter();
  const { login } = useAuth();

  // 处理登录
  const handleLogin = async (values: LoginRequest) => {
    setLoading(true);
    try {
      const response = await authLogin(values);
      console.log(response,'response');
      
      if (response && response.token && response.user) {
        // 使用 AuthContext 的 login 方法
        login(response.token, response.user);
        
        message.success('登录成功');
        
        // AuthContext的路由守卫会自动处理跳转，无需手动跳转
      } else {
        console.error('登录响应格式错误:', response);
        message.error('登录响应格式错误');
      }
    } catch (error) {
      message.error('登录失败，请检查用户名和密码');
      console.log(error,'error');
      
    } finally {
      setLoading(false);
    }
  };

  return (
    <div className={styles.loginPage}>
      <Card
        className={styles.loginCard}
        bodyStyle={{ padding: '40px 32px' }}
      >
        <div className={styles.loginTitleContainer}>
          <SafetyCertificateOutlined
            className={styles.loginTitleIcon}
          />
          <Title level={2} className={styles.loginTitle}>
            后台管理系统
          </Title>
          <Text type="secondary">个性化学习路径规划平台</Text>
        </div>

        <Form
          form={form}
          name="login"
          onFinish={handleLogin}
          size="large"
          layout="vertical"
        >
          <Form.Item
            name="username"
            rules={[
              { required: true, message: '请输入用户名或邮箱' },
            ]}
          >
            <Input
              prefix={<UserOutlined />}
              placeholder="用户名或邮箱"
              className={styles.loginFormInput}
            />
          </Form.Item>

          <Form.Item
            name="password"
            rules={[
              { required: true, message: '请输入密码' },
            ]}
          >
            <Input.Password
              prefix={<LockOutlined />}
              placeholder="密码"
              className={styles.loginFormInput}
            />
          </Form.Item>

          <Form.Item style={{ marginBottom: 16 }}>
            <Button
              type="primary"
              htmlType="submit"
              loading={loading}
              block
              className={styles.loginFormButton}
            >
              登录
            </Button>
          </Form.Item>
        </Form>

        <Divider className={styles.divider}>
          <Text type="secondary" className={styles.dividerText}>
            系统功能
          </Text>
        </Divider>




      </Card>
    </div>
  );
}